package com.yzf.demo.demo.Thread;

import java.util.concurrent.TimeUnit;

/**
 * Created by 于占峰 on 2020/3/31/031.
 * Thread.interrupted方法对设置中断标识的线程复位,并返回当前的中断状态
 */
public class InterruptedDemo {

    private static int i;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {
                //true标识被其他线程中断过
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("before:" + Thread.currentThread().isInterrupted());
                    Thread.interrupted();//将中断标识符复位为false
                    System.out.println("after:" + Thread.currentThread().isInterrupted());
                }
            }
        });
        thread.start();
        TimeUnit.SECONDS.sleep(1);
        thread.interrupt();//中断
    }
}
